<?php

use think\migration\Migrator;
use think\migration\db\Column;
use Phinx\Db\Adapter\MysqlAdapter;

class Exams extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function up()
    {
        $this->table('question_exams')
            ->addColumn('question_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'comment' => '题目ID'])
            ->addColumn('examinee_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'comment' => '受试者用户ID'])
            ->addColumn('result', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'comment' => '受试结果，1正确，2错误'])
            ->addColumn('create_at', 'datetime')
            ->addColumn('update_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
            ->addIndex(['question_id'])
            ->addIndex(['examinee_id'])
            ->addIndex(['question_id', 'examinee_id'], ['unique' => true]) // 保证一个人做一个题只有一个结果
            ->addIndex(['result'])
            ->addIndex(['question_id', 'result'])
            ->save();
    }

    public function down()
    {
        $this->table('question_exams')->drop();
    }
}
